package com.sc.nft.admin.controller;

import cn.hutool.core.map.MapUtil;
import com.sc.nft.admin.request.SearchDigitalCollectionRequest;
import com.sc.nft.admin.request.SyncSecondCollectionDataRequest;
import com.sc.nft.admin.request.SyncSecondOrderDataRequest;
import com.sc.nft.annotation.SysMemberLogin;
import com.sc.nft.dao.DigitalCollectionDao;
import com.sc.nft.dao.SecondCollectionDao;
import com.sc.nft.dao.SecondOrderDao;
import com.sc.nft.sup.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Slf4j
@RestController
@Api(tags = "数据同步")
@RequiredArgsConstructor
@RequestMapping("/admin/sync_data")
public class SyncDataAdminController {

    private final SecondOrderDao secondOrderDao;
    private final SecondCollectionDao secondCollectionDao;
    private final DigitalCollectionDao digitalCollectionDao;
    private final ThreadPoolTaskExecutor executor;

    /**
     * 关键字搜索数字藏品
     *
     * @param request 数字藏品名称
     */
    @SysMemberLogin
    @PostMapping(value = "/search_digital_collection", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "搜索藏品", notes = "搜索藏品", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    public Result<List<Map<String, Object>>> searchDigitalCollection(@RequestBody SearchDigitalCollectionRequest request) {
        return Result.success(
                digitalCollectionDao.findByKeyword(request.getKeyword()).stream()
                        .map(entity -> MapUtil.<String, Object>builder()
                                .put("id", entity.getId())
                                .put("firstTitle", entity.getFirstTitle())
                                .build()
                        )
                        .collect(Collectors.toList())
        );
    }

    @SysMemberLogin
    @PostMapping(value = "/sync_second_collection_data", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "同步寄售数据", notes = "同步寄售数据", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    public Result<?> syncSecondCollectionData(@RequestBody SyncSecondCollectionDataRequest request) {
        log.info("同步寄售数据 collectionId: {}, userTel: {}", request.getCollectionId(), request.getUserTel());
        executor.execute(() -> secondCollectionDao.syncToEs(request.getCollectionId(), request.getUserTel()));
        return Result.success();
    }

    @SysMemberLogin
    @PostMapping(value = "/sync_second_order_data", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "同步二级订单数据", notes = "同步二级订单数据", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    public Result<?> syncSecondOrderData(@RequestBody SyncSecondOrderDataRequest request) {
        log.info("同步二级订单数据 collectionId: {}, userTel: {}", request.getCollectionId(), request.getUserTel());
        executor.execute(() -> secondOrderDao.syncToEs(request.getCollectionId(), request.getUserTel()));
        return Result.success();
    }

}
